import os
import sys
import datetime
import utils
import xmmsclient
import xmmsqt4
from PyQt4 import QtCore, QtGui
from playController_ui import Ui_Form
from xmmsclient import collections


class XMMS2Model:
    def __init__(self, xmms2):
        self.xmms2 = xmms2

    def next(self):
        self.xmms2.playlist_current_pos(self, next_cb)

    def pause(self):
        self.xmms2.playback_pause()
    
    def play(self):
        self.xmms2.playback_start()

    def previous(self):
        pass
    
    def stop(self):
        self.xmms2.playback_stop()

    def seek(self, value):
        print value
        pass

    def stopBroadcastPosition(self):
        pass
    

class PlayController(Ui_Form, QtGui.QWidget):
    broadcastPosition = True
    
    def __init__(self, xmms2):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
        self.xmms2 = xmms2
        self.volume()
        self.trackDuration()
        self.xmms2.broadcast_playback_current_id(cb=self.updateCurrentTrack)
        self.xmms2.signal_playback_playtime(self.trackPosition)

    def continueBroadcastPosition(self):
        milliSeconds = self.positionSlider.value()
        self.xmms2.playback_seek_ms(milliSeconds)
        self.broadcastPosition = True
        
    def next(self):
        self.xmms2.playlist_set_next_rel(1)
        self.xmms2.playback_tickle()

    def pause(self):
        self.xmms2.playback_pause()
    
    def play(self):
        self.xmms2.playback_start()

    def previous(self):
        self.xmms2.playlist_set_next_rel(-1)
        self.xmms2.playback_tickle()
    
    def stop(self):
        self.xmms2.playback_stop()

    def seek(self, value):
        print value
        pass

    def stopBroadcastPosition(self):
        self.broadcastPosition = False

    def setVolume(self, value):
        self.xmms2.playback_volume_set("master", value)
        
    def trackDuration(self, id=None):
        def setDuration(res):
            if res.iserror(): return "error getting current track duration"
            info, = res.value()
            duration = info["duration"]
            self.positionSlider.setRange(0, duration)
        def getCurrentDuration(res):
            if res.iserror(): return "error getting current track duration"
            id = unicode(res.value())
            self.xmms2.coll_query_infos(collections.Match(field="id", value=id),
                                        ["duration"],
                                        cb=setDuration)
        if id is None:
            self.xmms2.playback_current_id(getCurrentDuration)
        else:
            self.xmms2.coll_query_infos(collections.Match(field="id", value=id),
                                        ["duration"],
                                        cb=setDuration)

    def trackPosition(self, res):
        if res.iserror(): return "could not get playtime signal"
        playtime = res.value()
        time = utils.msecToStr(playtime)
        self.lcdNumber.display(time)
        if self.broadcastPosition: self.positionSlider.setValue(playtime)
        res.restart()
        
    def volume(self):
        def volume_cb(res):
            if res.iserror(): return "error retrieving volume"
            masterVol = res.value()["master"]
            self.dial.setValue(masterVol)
        self.xmms2.playback_volume_get(volume_cb)
    
    def updateCurrentTrack(self, res):
        if res.iserror(): return "error updating playController to current track"
        info = res.value()
        self.trackDuration()

        
if __name__ == "__main__":
    xmms2 = xmmsclient.XMMS("playcontroller")
    try:
        xmms2.connect(os.getenv("XMMSPATH"))
    except IOError, detail:
        print "Could not connect to xmms2!", detail
        exit(1)

    app = QtGui.QApplication(sys.argv)
    xmmsqt4.XMMSConnector(xmms2, app)    
    playController = PlayController(xmms2)
    playController.show()
    sys.exit(app.exec_())
    
